#!/usr/bin/env python
# coding: utf-8
# $Id$
# http request consumer

import parallels
from twisted.internet import defer
from twisted.web.client import getPage

def reply(content, host, deferred):
    item = {'host':host, 'content':content}
    deferred.callback(item)

class consumer(parallels.ConsumerProtocol):
    def get(self, item):
	# Here the consumer creates a deferred, which will be
	# returned by the get() method. This is done because
	# we don't want to block the reactor.
	deferred = defer.Deferred()

	# The getPage() will return yet another deferred, and
	# the callback on it will be fired once the page content
	# is received. When that happen, we then fire our previously
	# created deferred's callback() method with the data to be
	# replied to the Producer on it.
	#
	# It's just like the Twisted XMLRPC server.
	page = getPage('http://freegeoip.net/xml/' + item['addr'])
	page.addCallback(reply, item['host'], deferred)
	return deferred

if __name__ == '__main__':
    consumer(runReactor=True)
